/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomerPanel.java
 *
 * Created on Feb 3, 2009, 7:13:25 AM
 */

package trader.gui;

import javax.swing.JPanel;
import trader.*;

/**
 *
 * @author jt
 */
public class CustomerPanel
extends javax.swing.JPanel
implements BrokerPanel
{
    //Instance Vars
    private BrokerController controller;
    private BrokerModel      model;

    /** Creates new form CustomerPanel */
    public CustomerPanel() {
        initComponents();
    }//CustomerPanel Constructor

//Methods Implemented from BrokerPanel

    /**
     * Register a controller for this view
     *
     * @param controller    The controller to register
     */
    public void registerController(BrokerController controller) {
        this.controller = controller;
    }//registerController()

    /**
     * Registers a model for this view
     *
     * @param model     The model to register
     */
    public void registerModel(BrokerModel model) {
        this.model = model;
    }//registerModel()

    /**
     * Refresh the current customer data, or get a new customer if
     * the customer ID has changed.
     * <br /><br />
     * <b>refresh()</b> uses the <b>display()</b> method to acutally
     * display the refreshed customer data.
     *
     * @see display()
     */
    public void refresh() {
        if (model != null){
            try {
                Customer customer = model.getCustomer(idTf.getText());
                display(customer);
            }//try
            catch(Exception e){
                idTf.selectAll();
                //TODO Send stack trace to the logging panel when available
            }//catch()
        }//if
    }//refresh()

    /**
     * Verifies the object passed is a Customer object, then displays
     * the customer data in the appropriate text fields.
     *
     * @param obj   The Customer object to display
     */
    public void display(Object obj) {
        if (obj instanceof Customer){
            Customer c = (Customer)obj;
            nameTf.setText(c.getName());
            idTf.setText(c.getId());
            addrTf.setText(c.getAddr());
        }//if
    }//display()

    /**
     * Returns a reference to 'this' view
     *
     * @return  A reference to 'this' view
     */
    public JPanel getPanel() {
        return this;
    }//getPanel()

    ///////////////////////////////////////////////////////////////////////////
    // Newly added Helper Classes
    // Seminar 5 Assignment III
    ///////////////////////////////////////////////////////////////////////////

    public String getCustId(){
        return idTf.getText().trim();
    }
    public String getCustName(){
        return nameTf.getText().trim();
    }
    public String getCustAddr(){
        return addrTf.getText().trim();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpnFields = new javax.swing.JPanel();
        nameLb = new javax.swing.JLabel();
        nameTf = new javax.swing.JTextField();
        idLb = new javax.swing.JLabel();
        idTf = new javax.swing.JTextField();
        addrLb = new javax.swing.JLabel();
        addrTf = new javax.swing.JTextField();
        jpnButtons = new javax.swing.JPanel();
        getBt = new javax.swing.JButton();
        updBt = new javax.swing.JButton();
        addBt = new javax.swing.JButton();
        delBt = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout(5, 5));

        nameLb.setText("Customer Name");

        idLb.setText("Customer Identity");

        addrLb.setText("Customer Address");

        javax.swing.GroupLayout jpnFieldsLayout = new javax.swing.GroupLayout(jpnFields);
        jpnFields.setLayout(jpnFieldsLayout);
        jpnFieldsLayout.setHorizontalGroup(
            jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnFieldsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jpnFieldsLayout.createSequentialGroup()
                        .addComponent(addrLb)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addrTf, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpnFieldsLayout.createSequentialGroup()
                        .addGroup(jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nameLb)
                            .addComponent(idLb))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(idTf, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(nameTf, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        jpnFieldsLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addrLb, idLb, nameLb});

        jpnFieldsLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addrTf, idTf, nameTf});

        jpnFieldsLayout.setVerticalGroup(
            jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnFieldsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLb)
                    .addComponent(nameTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(idLb))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnFieldsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addrLb)
                    .addComponent(addrTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jpnFields, java.awt.BorderLayout.CENTER);

        jpnButtons.setLayout(new java.awt.GridLayout(2, 2, 5, 5));

        getBt.setText("Get Customer");
        getBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getBtActionPerformed(evt);
            }
        });
        jpnButtons.add(getBt);

        updBt.setText("Update Customer");
        updBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updBtActionPerformed(evt);
            }
        });
        jpnButtons.add(updBt);

        addBt.setText("Add Customer");
        addBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBtActionPerformed(evt);
            }
        });
        jpnButtons.add(addBt);

        delBt.setText("Delete Customer");
        delBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delBtActionPerformed(evt);
            }
        });
        jpnButtons.add(delBt);

        add(jpnButtons, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    //////////////////////////////////////////////////////////////////////////
    // Newly added handler code
    // Seminar 5 Assignment III
    //////////////////////////////////////////////////////////////////////////
    /**
     * Get Button Action
     * @param evt   This event data
     */
    private void getBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getBtActionPerformed
        String buttonName = evt.getActionCommand();
        String custId     = getCustId();
        System.out.printf("CustomerListener buttonName = %s, ID: %s\n",
                           buttonName, custId);
        controller.handleGetCustomerGesture(custId);
    }//GEN-LAST:event_getBtActionPerformed
    /**
     * Update Button Action
     * @param evt   This event data
     */
    private void updBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updBtActionPerformed
        String buttonName = evt.getActionCommand();
        String custId     = getCustId();
        Customer cust     = new Customer(custId, getCustName(), getCustAddr());
        System.out.printf("CustomerListener buttonName = %s, ID: %s\n",
                           buttonName, custId);
        controller.handleUpdateCustomerGesture(cust);
    }//GEN-LAST:event_updBtActionPerformed
    /**
     * Add Button Action
     * @param evt   This event data
     */
    private void addBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBtActionPerformed
        String buttonName = evt.getActionCommand();
        String custId     = getCustId();
        Customer cust     = new Customer(custId, getCustName(), getCustAddr());
        System.out.printf("CustomerListener buttonName = %s, ID: %s\n",
                           buttonName, custId);
        controller.handleAddCustomerGesture(cust);
    }//GEN-LAST:event_addBtActionPerformed
    /**
     * Delete Button Action
     * @param evt   This event data
     */
    private void delBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delBtActionPerformed
        String buttonName = evt.getActionCommand();
        String custId     = getCustId();
        Customer cust     = new Customer(custId, getCustName(), getCustAddr());
        System.out.printf("CustomerListener buttonName = %s, ID: %s\n",
                           buttonName, custId);
        controller.handleDeleteCustomerGesture(cust);
    }//GEN-LAST:event_delBtActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addBt;
    private javax.swing.JLabel addrLb;
    private javax.swing.JTextField addrTf;
    private javax.swing.JButton delBt;
    private javax.swing.JButton getBt;
    private javax.swing.JLabel idLb;
    private javax.swing.JTextField idTf;
    private javax.swing.JPanel jpnButtons;
    private javax.swing.JPanel jpnFields;
    private javax.swing.JLabel nameLb;
    private javax.swing.JTextField nameTf;
    private javax.swing.JButton updBt;
    // End of variables declaration//GEN-END:variables

}
